extends Card

# 血肉契约誓言卡牌
# 功能：打出后，使所有卡的cost增加一点，效果翻倍

@export var cost_increase: int = 1  # cost增加量
@export var effect_multiplier: float = 2.0  # 效果翻倍倍数
@export var duration: int = 3  # 持续回合数

func _init():
	# 设置卡牌基础属性
	id = "fleshpact_oath"
	type = Type.SKILL
	rarity = Rarity.LEGEND
	target = Target.SELF
	# 以下属性现在由 .tres 文件设置，不再硬编码
	# cost, cost_increase, effect_multiplier, duration, card_max_repeat_num, card_current_repeat_num
	
	# 设置描述文本
	tooltip_text = "[center]血肉契约誓言：激活后，所有卡牌cost+%s，效果翻倍，持续%s回合[/center]"

func get_default_tooltip() -> String:
	# 使用replace方法替换%s占位符
	var formatted_text = tooltip_text
	formatted_text = formatted_text.replace("%s", str(cost_increase))
	return formatted_text

func get_updated_tooltip(_player_modifiers: ModifierHandler, _enemy_modifiers: ModifierHandler) -> String:
	# 动态更新提示文本，显示当前的cost增加和效果翻倍
	var formatted_text = tooltip_text
	formatted_text = formatted_text.replace("%s", str(cost_increase))
	
	# 添加当前状态信息
	formatted_text += "\n\n🚀 激活状态："
	formatted_text += "\n  - Cost增加: +%d" % cost_increase
	formatted_text += "\n  - 效果翻倍: %.1fx" % effect_multiplier
	formatted_text += "\n  - 持续时间: %d回合" % duration
	
	return formatted_text

func apply_effects(targets: Array[Node], modifiers: ModifierHandler) -> void:
	print("🩸 血肉契约誓言卡牌被使用！")
	
	for target in targets:
		if target is Player:
			_activate_fleshpact_oath(target)
			
			if sound:
				SFXPlayer.play(sound)

func _activate_fleshpact_oath(player: Player) -> void:
	print("🩸 血肉契约誓言激活！")
	
	# 1. 添加cost增加修改器
	_apply_cost_increase_modifier(player)
	
	# 2. 添加效果翻倍修改器
	_apply_effect_multiplier_modifier(player)
	
	# 3. 安排自动移除
	_schedule_modifier_removal(player)
	
	# 4. 通知手牌更新所有卡牌的费用显示
	_notify_hand_update_costs(player)

func _apply_cost_increase_modifier(player: Player) -> void:
	# 获取或创建cost增加修改器
	# 注意：player是Player节点，应该直接访问modifier_handler
	if player.get("modifier_handler") == null:
		print("❌ 玩家没有modifier_handler")
		return
		
	var cost_modifier = player.modifier_handler.get_modifier(Modifier.Type.CARD_COST)
	if not cost_modifier:
		cost_modifier = Modifier.new()
		cost_modifier.type = Modifier.Type.CARD_COST
		player.modifier_handler.add_child(cost_modifier)
	
	# 创建cost增加值
	var cost_increase_value = ModifierValue.create_new_modifier("fleshpact_oath_cost", ModifierValue.Type.FLAT)
	cost_increase_value.flat_value = cost_increase
	
	cost_modifier.add_new_value(cost_increase_value)
	
	print("💰 已为 %s 添加cost增加修改器，增加: +%d" % [player.name, cost_increase])

func _apply_effect_multiplier_modifier(player: Player) -> void:
	# 获取或创建效果翻倍修改器
	# 注意：player是Player节点，应该直接访问modifier_handler
	if player.get("modifier_handler") == null:
		print("❌ 玩家没有modifier_handler")
		return
		
	var multiplier_modifier = player.modifier_handler.get_modifier(Modifier.Type.CARD_EFFECT_MULTIPLIER)
	if not multiplier_modifier:
		multiplier_modifier = Modifier.new()
		multiplier_modifier.type = Modifier.Type.CARD_EFFECT_MULTIPLIER
		player.modifier_handler.add_child(multiplier_modifier)
		print("🔧 为玩家创建CARD_EFFECT_MULTIPLIER修改器")
	
	# 创建翻倍值
	var multiplier_value = ModifierValue.create_new_modifier("fleshpact_oath_multiplier", ModifierValue.Type.PERCENT_BASED)
	# 对于翻倍效果，我们需要设置percent_value为1.0（100%），这样1 + 1.0 = 2.0倍
	multiplier_value.percent_value = effect_multiplier - 1.0  # 2.0倍 = 100%增加
	
	multiplier_modifier.add_new_value(multiplier_value)
	
	print("🚀 已为 %s 添加效果翻倍修改器，倍数: %.1f" % [player.name, effect_multiplier])
	print("🔧 详细调试信息:")
	print("  - effect_multiplier: %.1f" % effect_multiplier)
	print("  - percent_value: %.1f" % multiplier_value.percent_value)
	print("  - 预期结果: 1 + %.1f = %.1f倍" % [multiplier_value.percent_value, effect_multiplier])
	
	# 验证修改器是否正确应用
	var test_value = multiplier_modifier.get_modified_value(1)
	print("🔧 验证测试: get_modified_value(1) = %d" % test_value)

func _schedule_modifier_removal(player: Player) -> void:
	# 使用现有的调度系统安排移除
	var tree = player.get_tree()
	var battle = tree.get_first_node_in_group("battle")
	if battle and battle.resource_scheduler:
		var removal_effect = ScheduledEffect.new()
		removal_effect.id = "fleshpact_oath_removal"
		removal_effect.trigger_type = ScheduledEffect.TriggerType.TURN_COUNT
		removal_effect.target_type = ScheduledEffect.TargetType.PLAYER
		removal_effect.trigger_turn = battle.current_turn + duration
		removal_effect.is_repeating = false
		removal_effect.max_triggers = 1
		
		# 创建移除效果
		var removal_effect_instance = FleshpactOathRemovalEffect.new()
		removal_effect_instance.player = player
		removal_effect.custom_effects.append(removal_effect_instance)
		
		battle.resource_scheduler.add_scheduled_effect(removal_effect)
		
		print("📅 已安排在第 %d 回合移除血肉契约誓言效果" % removal_effect.trigger_turn) 

# 新增：通知手牌更新费用显示
func _notify_hand_update_costs(player: Player) -> void:
	# 方法1：通过player_handler访问手牌
	var player_handler = null
	var tree = player.get_tree()
	if tree:
		player_handler = tree.get_first_node_in_group("player_handler")
		if player_handler:
			if player_handler.get("hand") != null:
				var hand = player_handler.hand
				if hand.has_method("update_all_card_costs"):
					hand.update_all_card_costs()
					print("🔧 已通知手牌更新所有卡牌的费用显示")
				else:
					print("❌ 手牌没有update_all_card_costs方法")
			else:
				print("❌ player_handler没有hand引用")
		else:
			print("❌ 未找到player_handler节点")
	else:
		print("❌ 无法获取场景树") 
